unit fEdit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ZAbstractTable, ZDataset, DB, ZAbstractRODataset, ZAbstractDataset,
  ZConnection, ComCtrls, StdCtrls, Setting, Grids, ExtCtrls;
type
  TPenyimpan = record
     ID:String;
     NAMA:String;
  end;
  TTPenyimpan = array of TPenyimpan;
  TDpt = record
     KODE:String;
     NAMA:String;
  end;
  TTDpt = array of TDpt;
  TForm3 = class(TForm)
    Button1: TButton;
    GroupBox1: TGroupBox;
    Label6: TLabel;
    Label3: TLabel;
    Label5: TLabel;
    Label9: TLabel;
    Label12: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label18: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    Label24: TLabel;
    Label28: TLabel;
    Label30: TLabel;
    Label32: TLabel;
    Label33: TLabel;
    Label36: TLabel;
    TextNIK: TEdit;
    TextKK: TEdit;
    TextNama: TEdit;
    TextTempat: TEdit;
    TextAgama: TEdit;
    TextAlamat: TEdit;
    TextRW: TEdit;
    TextRT: TEdit;
    TextKeterangan: TEdit;
    cbKabkot: TComboBox;
    cbKec: TComboBox;
    cbDesa: TComboBox;
    cbJk: TComboBox;
    cbKawin: TComboBox;
    ZConnection1: TZConnection;
    ZQuery1: TZQuery;
    ZTable1: TZTable;
    txtDusun: TEdit;
    Label26: TLabel;
    Label37: TLabel;
    txtUsia: TEdit;
    cbTgl: TComboBox;
    cbBulan: TComboBox;
    txtTahun: TEdit;
    sgHistory: TStringGrid;
    GroupBox2: TGroupBox;
    GroupBox3: TGroupBox;
    Label1: TLabel;
    cbPerbaikan: TComboBox;
    Label2: TLabel;
    ZTable2: TZTable;
    Label4: TLabel;
    cbPemilih: TComboBox;
    cbCacat: TComboBox;
    btnBatal: TButton;
    Label7: TLabel;
    cbPend: TComboBox;
    cbPek: TComboBox;
    Label8: TLabel;
    cbStatR: TComboBox;
    Label10: TLabel;
    cbWN: TComboBox;
    Label11: TLabel;
    Label13: TLabel;
    txtTPS: TEdit;
    GroupBox4: TGroupBox;
    cbT1: TComboBox;
    cbT2: TComboBox;
    cbT3: TComboBox;
    cbT4: TComboBox;
    Button2: TButton;
    Button3: TButton;
    Label14: TLabel;
    Label17: TLabel;
    Label19: TLabel;
    Label22: TLabel;
    Label23: TLabel;
    Button4: TButton;
    Timer1: TTimer;
    Button5: TButton;
    kembaliButton: TButton;
    Label25: TLabel;
    Label27: TLabel;
    TextIbu: TEdit;
    cbTPS: TComboBox;
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure TextNIKKeyPress(Sender: TObject; var Key: Char);
    procedure TextNamaKeyPress(Sender: TObject; var Key: Char);
    procedure cbKabkotChange(Sender: TObject);
    procedure cbKecChange(Sender: TObject);
    procedure cbDesaChange(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnTPSClick(Sender: TObject);
    procedure btnBatalClick(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure cbT1Change(Sender: TObject);
    procedure cbT2Change(Sender: TObject);
    procedure cbT3Change(Sender: TObject);
    procedure cbT4Change(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure sgHistoryClick(Sender: TObject);
    procedure kembaliButtonClick(Sender: TObject);
    function HandleSpec(Asal: String):String;
    procedure cbTPSChange(Sender: TObject);
  private
    function cekForm: Boolean;
    procedure KeyOnlyNum(Sender: TObject; var Key: Char);
    function getTgl: String;
    procedure AutoSizeCol(Grid: TStringGrid; Column: integer);
    procedure BukanGanda;
    procedure HapusPemilih;
    procedure HapusTanda;
    procedure PindahAlamat;
    procedure UbahTanda(td : String);
    function Hitung : integer;
    function getDesa(kode: String): String;
    function getIdsimpan(tabel: String): String;
    function getIduser(id: String): String;
    function getKabkot(kode: String): String;
    function getKec(kode: String): String;
    function getSimpan(id: String): String;
    function getTabel(id: String): String;
    function ketAP(id: integer): String;
    function ketCa(id: integer): String;
    function ketGanda(id: integer): String;
    function ketPek(id: integer): String;
    function ketPend(id: integer): String;
    function ketS1(id: integer): String;
    function ketS2(id: integer): String;
    function ketSP(id: integer): String;
    function ketSR(id: integer): String;
    function ketTPS(id: String): String;
    function ketWN(id: integer): String;
    { Private declarations }
  public
    { Public declarations }
    constructor Create(AOwner: TComponent; tabel: String; ID:String; IDT:String; isganda:Boolean; bta:Integer; kodelog:String;user:String=''; tabelx: String=''; IDX:String=''; IDTX:String='');             overload;
  end;

var
  Form3: TForm3;
  bPenyimpan:TTPenyimpan;
  bDesa:TTDpt;
  bKec:TTDpt;
  bKabkot:TTDpt;
  ptabel,ptabelx,pid,pidx,tid,tidx:String;
  atb:Integer;
  ganda:Boolean;
  ikabkot:String;
  ikec:String;
  idesa:String;
  itps, stps:String;
  sKabkot,sKabkot2: String;
  sKec,sKec2,skodekab,skodekec: String;
  sDesa,sDesa2,idTPS,idTPS2: String;
  username:String;
  daftarHistori:tStringList;

implementation
{$R *.dfm}
procedure TForm3.cbDesaChange(Sender: TObject);
var
  pilihan: String;
  i,idx:integer;
begin
  pilihan := cbDesa.Items[cbDesa.ItemIndex];
  with ZTable1 do
  begin
    Active := False;
    TableName := 'desa';
    Filter := 'TAMPIL = ' + QuotedStr(pilihan) + ' and KODE_KECAMATAN = ' +
      QuotedStr(sKec);
    Filtered := True;
    Active := True;
    First;
    sDesa := FieldByName('KODE_DESA').AsString;
    //showmessage(sdesa);
  end;

  //TPS
  stps:='0';
  with ZTable1 do
  begin
    Active := False;
    TableName := 'daftar_tps';
    Filter := 'KODE_DESA='+QuotedStr(sdesa);
    Filtered:=True;
    Active := True;
    First;
  end;
  cbTPS.Clear;
  cbTPS.Items.Add('');
  i:=0;
  idx:=0;
  while (not ZTable1.Eof) do
  begin
    cbTPS.Items.Add(ZTable1.FieldByName('NAMA').AsString);
    if (itps=ZTable1.FieldByName('ID_TPS').AsString) then begin
      idx:=i;
      //showmessage(inttostr(i));
      //sdesa:=ZTable1.FieldByName('KODE_DESA').AsString;
      stps:=ZTable1.FieldByName('ID_TPS').AsString;
    end;
    ZTable1.Next;
    Inc(i);
  end;
  cbTPS.ItemIndex:=idx;
end;

function TForm3.HandleSpec(Asal: String):String;
var
  temp:string;
begin
  temp := StringReplace(asal, '''', '',[rfReplaceAll, rfIgnoreCase]);
  temp := StringReplace(temp, '\', '',[rfReplaceAll, rfIgnoreCase]);
  result := temp;
end;

procedure TForm3.cbKabkotChange(Sender: TObject);
var
  pilihan: String;
begin
  pilihan := cbKabkot.Items[cbKabkot.ItemIndex];
  with ZTable1 do
  begin
    Active := False;
    TableName := 'kabkot';
    Filter := 'TAMPIL = ' + QuotedStr(pilihan);
    Filtered := True;
    Active := True;
    First;
    sKabkot := FieldByName('KODE').AsString;
    // cb2
    Active := False;
    TableName := 'kecamatan';
    Filter := 'KODE_KABKOT = ' + QuotedStr(sKabkot);
    Filtered := True;
    Active := True;
    First;
  end;
  cbKec.Clear;
  cbDesa.Clear;
  while (not ZTable1.Eof) do
  begin
    cbKec.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
    ZTable1.Next;
  end;
end;

procedure TForm3.cbKecChange(Sender: TObject);
var
  pilihan: String;
begin
  pilihan := cbKec.Items[cbKec.ItemIndex];
  with ZTable1 do
  begin
    Active := False;
    TableName := 'kecamatan';
    Filter := 'TAMPIL = ' + QuotedStr(pilihan) + ' and KODE_KABKOT = ' + QuotedStr
      (sKabkot);
    Filtered := True;
    Active := True;
    First;
    sKec := FieldByName('KODE_KECAMATAN').AsString;
    // cb2
    Active := False;
    TableName := 'desa';
    Filter := 'KODE_KECAMATAN = ' + QuotedStr(sKec);
    Filtered := True;
    Active := True;
    First;
  end;
  cbDesa.Clear;
  while (not ZTable1.Eof) do
  begin
    cbDesa.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
    ZTable1.Next;
  end;
end;

procedure TForm3.cbT1Change(Sender: TObject);
var
  pilihan: String;
begin
  pilihan := cbT1.Items[cbT1.ItemIndex];
  with ZTable1 do
  begin
    Active := False;
    TableName := 'kabkot';
    Filter := 'TAMPIL = ' + QuotedStr(pilihan);
    Filtered := True;
    Active := True;
    First;
    skabkot2 := FieldByName('KODE').AsString;
    // cb2
    Active := False;
    TableName := 'kecamatan';
    Filter := 'KODE_KABKOT = ' + QuotedStr(skabkot2);
    Filtered := True;
    Active := True;
    First;
  end;
  cbT2.Clear;
  cbT3.Clear;
  cbT4.Clear;
  while (not ZTable1.Eof) do
  begin
    cbT2.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
    ZTable1.Next;
  end;
end;

procedure TForm3.cbT2Change(Sender: TObject);
var
  pilihan: String;
begin
  pilihan := cbT2.Items[cbT2.ItemIndex];
  with ZTable1 do
  begin
    Active := False;
    TableName := 'kecamatan';
    Filter := 'TAMPIL = ' + QuotedStr(pilihan) + ' and KODE_KABKOT = ' + QuotedStr
      (sKabkot2);
    Filtered := True;
    Active := True;
    First;
    sKec2 := FieldByName('KODE_KECAMATAN').AsString;
    // cb2
    Active := False;
    TableName := 'desa';
    Filter := 'KODE_KECAMATAN = ' + QuotedStr(sKec2);
    Filtered := True;
    Active := True;
    First;
  end;
  cbT3.Clear;
  cbT4.Clear;
  while (not ZTable1.Eof) do
  begin
    cbT3.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
    ZTable1.Next;
  end;
end;

procedure TForm3.cbT3Change(Sender: TObject);
var
  pilihan: String;
begin
  pilihan := cbT3.Items[cbT3.ItemIndex];
  with ZTable1 do
  begin
    Active := False;
    TableName := 'desa';
    Filter := 'TAMPIL = ' + QuotedStr(pilihan) + ' and KODE_KECAMATAN = ' +
      QuotedStr(sKec2);
    Filtered := True;
    Active := True;
    First;
    sDesa2 := FieldByName('KODE_DESA').AsString;
    //
    Active := False;
    TableName := 'daftar_tps';
    Filter := 'KODE_DESA='+QuotedStr(sdesa2);
    Filtered:=True;
    Active := True;
    First;
  end;
  cbT4.Clear;
  while (not ZTable1.Eof) do
  begin
    cbT4.Items.Add(ZTable1.FieldByName('NAMA').AsString);
    ZTable1.Next;
  end;
end;

procedure TForm3.cbT4Change(Sender: TObject);
var
  pilihan: String;
begin
  pilihan := cbT4.Items[cbT4.ItemIndex];
  with ZTable1 do
  begin
    Active := False;
    TableName := 'daftar_tps';
    Filter := 'NAMA = ' + QuotedStr(pilihan) + ' and KODE_DESA = ' +
      QuotedStr(sDesa2);
    Filtered := True;
    Active := True;
    First;
    idTPS2 := FieldByName('ID_TPS').AsString;
  end;
  Button2.Enabled:=true;
end;

Function TForm3.cekForm() : Boolean;
var
  sTemp:Boolean;
begin
  sTemp:=False;
  if ((TextNIK.Text<>'') and (TextNama.Text<>'') and (TextRW.Text<>'') and (TextRT.Text<>'') and (cbKabkot.ItemIndex>-1) and (cbKec.ItemIndex>-1) and (cbDesa.ItemIndex>-1) and (cbJk.ItemIndex>-1) and (cbKawin.ItemIndex>-1)) then begin
    if (txtUsia.Text<>'') then
      sTemp:=True
    else if ((cbTgl.ItemIndex>0) and (cbBulan.ItemIndex>0) and (txtTahun.Text<>'')) then
      sTemp:=True
  end;
  Result := sTemp;
end;

Function cekRTRW(kata:String) : String;
var
  stemp:String;
begin
  sTemp:='';
  if (Length(kata)=1) then
    sTemp:='00'+kata
  else if (Length(kata)=2) then
    sTemp:='0'+kata
  else if (Length(kata)>3) then
    sTemp:=Copy(kata, Length(kata)-3, 3)
  else
    sTemp:=kata;
  Result := sTemp;
end;

Function TForm3.getTgl() : String;
var
  sTemp,tgl,bln,thn:String;
  selisih:integer;
begin
  sTemp:='';
  if ((cbTgl.ItemIndex>0) and (cbBulan.ItemIndex>0) and (txtTahun.Text<>'')) then begin
    tgl:=cbTgl.Items[cbTgl.ItemIndex];
    if(Length(tgl) = 1) then
      tgl := '0'+tgl;
    bln:=cbBulan.Items[cbBulan.ItemIndex];
    if(Length(bln) = 1) then
      bln := '0'+bln;
    thn:=txtTahun.Text;
    sTemp:=tgl+'-'+bln+'-'+thn;
  end;
  Result := sTemp;
end;

procedure TForm3.Button1Click(Sender: TObject);
var
  values: TStringList;
  que,tanggal,idtgl:String;
  buttonSelected, buttonSelected2 : Integer;
  statushapus, kethapus : String;
  bob:Boolean;
  usiaSekarang: integer;
  tgllahir, itps, notps : String;
begin
  tgllahir := getTgl;
  usiaSekarang:= hitung;
  //showMessage(inttostr(usiaSekarang));
  if ((usiaSekarang < 17) AND (Copy(cbKawin.Items[cbKawin.ItemIndex],1,1) = 'B')) then begin
    buttonSelected := MessageDlg('Usia belum 17 tahun dan status belum menikah. Pemilih akan dihapus dari daftar pemilih secara otomatis. Apakah Anda yakin?',mtConfirmation , mbOKCancel, 0);
  end else if((cbPek.ItemIndex = 12) OR (cbPek.ItemIndex = 13)) then begin
    buttonSelected := MessageDlg('Pekerjaan yang dipilih TNI/Polri. Pemilih akan dihapus dari daftar pemilih secara otomatis. Apakah Anda yakin?',mtConfirmation , mbOKCancel, 0);    
  end else begin
    buttonSelected := MessageDlg('Apakah anda yakin akan mengubah data pemilih? Mohon dicek kembali data yang diisikan',mtConfirmation , mbOKCancel, 0);
  end;
  if buttonSelected = mrOK  then begin
    if(cbPerbaikan.Text <> '') then begin
    if((usiaSekarang < 17) AND (Copy(cbKawin.Items[cbKawin.ItemIndex],1,1) = 'B')) then begin
      statushapus := '6';
      kethapus:= '2';      
    end
    else if((cbPek.ItemIndex = 12) OR (cbPek.ItemIndex = 13)) then begin
      statushapus := '3';
      kethapus := '2';
    end
    else begin
      statushapus := '0';
      kethapus := '0';
    end;
//    showMessage(Copy(cbKawin.Items[cbKawin.ItemIndex],1,1));
//    showMessage(Copy(cbJk.Items[cbJk.ItemIndex],1,1));
    if (cekForm) then begin
      values := TStringList.Create;
      values.Add('0'); // dummy ID, no meaning
      // NIK`, `NKK`, `NAMA`, `TEMPAT`, `TANGGAL`, '+
      // '`IBU`, `AGAMA`, `KAWIN`, `KELAMIN`, `KELAINAN`, `ALAMAT`, `RW`, `RT`, `KET`, `ID_TANGGAL`
      values.Add(copy(HandleSpec(TextNIK.Text),1,36));
      values.Add(copy(HandleSpec(TextKK.Text),1,36));
      values.Add(copy(AnsiUpperCase(HandleSpec(TextNama.Text)),1,50));
      values.Add(copy(AnsiUpperCase(HandleSpec(TextTempat.Text)),1,36));
      values.Add(getTgl);
//      values.Add(AnsiUpperCase(TextIbu.Text));
      values.Add(copy(AnsiUpperCase(HandleSpec(TextAgama.Text)),1,36));
      values.Add(AnsiUpperCase(Copy(cbKawin.Items[cbKawin.ItemIndex],1,1)));
      values.Add(AnsiUpperCase(Copy(cbJk.Items[cbJk.ItemIndex],1,1)));
      values.Add(AnsiUpperCase(cbCacat.Items[cbcacat.ItemIndex]));
      values.Add(AnsiUpperCase(copy(HandleSpec(TextAlamat.Text),1,150)));
      values.Add(cbKabkot.Items[cbKabkot.ItemIndex]);
      values.Add(cbKec.Items[cbKec.ItemIndex]);
      values.Add(cbDesa.Items[cbDesa.ItemIndex]);
      values.Add(cekRTRW(TextRW.Text));
      values.Add(cekRTRW(TextRT.Text));
      values.Add(AnsiUpperCase(copy(HandleSpec(txtDusun.Text),1,30)));
      values.Add(AnsiUpperCase(copy(HandleSpec(TextKeterangan.Text),1,150)));
      //tps
      values.Add(stps);
      //statusp
      values.Add(IntToStr(cbPemilih.ItemIndex+1));
      //alasanp
      values.Add(IntToStr(cbPerbaikan.ItemIndex+1));
      //cacat
      values.Add(IntToStr(cbCacat.ItemIndex));
      //pendidikan
      values.Add(IntToStr(cbPend.ItemIndex+1));
      //pekerjaan
      values.Add(IntToStr(cbPek.ItemIndex+1));
      //statusr
      values.Add(IntToStr(cbStatR.ItemIndex+1));
      //wn
      values.Add(IntToStr(cbWN.ItemIndex+1));
      //ibu
      values.Add(HandleSpec(textIbu.Text));
      //tanggal
      tanggal := FormatDateTime('yyyy-mm-dd hh:mm:ss', Now());
      if((usiaSekarang < 17) AND (Copy(cbKawin.Items[cbKawin.ItemIndex],1,1) = 'B')) then begin
        values[17] := 'Di bawah umur';
      end;
      with ZQuery1 do  //masukkin nih gan
        begin
          Active:=False;
          sql.Clear;
          sql.Text := 'INSERT INTO tanggal (TANGGAL) VALUES ("'+tanggal+'")';
          ExecSQL;
        end;
      with ZQuery1 do  //masukkin nih gan
        begin
          Active:=False;
          sql.Clear;
          sql.Text := 'SELECT * FROM tanggal ORDER BY ID_TANGGAL DESC';
          Active:=True;
          idtgl := FieldByName('ID_TANGGAL').AsString;  //dapat ID_TGL
        end;
//      ShowMessage('idtgl='+idtgl);

      //---------------HISTORY
      with ZTable1 do begin
        active:=false;
        TableName:=ptabel;
        Filter:='ID_USER='+QuotedStr(pid);
        Filtered:=true;
        Active:=true;
        First;
      end;
      //showmessage(ptabel);
      with zquery1 do begin
          active:=false;
          sql.Clear;
          sql.Text:='INSERT INTO history (ID_USER,NIK,NKK,NAMA,TEMPAT,TANGGAL,IBU,AGAMA,KAWIN,KELAMIN,KELAINAN,ALAMAT,RW,RT,DUSUN,KET,ID_TANGGAL,TPS,STATUS1,STATUS2,GANDA,STATUS3,STATUSP,ALASANP,CACAT,PENDIDIKAN,PEKERJAAN,STATUSR,WN,USERNAME,KET_AKSI) VALUES ('+
            QuotedStr(tid+'-'+ZTable1.FieldByName('ID_USER').AsString)+','+
            QuotedStr(HandleSpec(ZTable1.FieldByName('NIK').AsString))+','+
            QuotedStr(HandleSpec(ZTable1.FieldByName('NKK').AsString))+','+
            QuotedStr(HandleSpec(ZTable1.FieldByName('NAMA').AsString))+','+
            QuotedStr(HandleSpec(ZTable1.FieldByName('TEMPAT').AsString))+','+
            QuotedStr(HandleSpec(ZTable1.FieldByName('TANGGAL').AsString))+','+
            QuotedStr(HandleSpec(ZTable1.FieldByName('IBU').AsString))+','+
            QuotedStr(HandleSpec(ZTable1.FieldByName('AGAMA').AsString))+','+
            QuotedStr(HandleSpec(ZTable1.FieldByName('KAWIN').AsString))+','+
            QuotedStr(HandleSpec(ZTable1.FieldByName('KELAMIN').AsString))+','+
            QuotedStr(HandleSpec(ZTable1.FieldByName('KELAINAN').AsString))+','+
            QuotedStr(HandleSpec(ZTable1.FieldByName('ALAMAT').AsString))+','+
            QuotedStr(HandleSpec(ZTable1.FieldByName('RW').AsString))+','+
            QuotedStr(HandleSpec(ZTable1.FieldByName('RT').AsString))+','+
            QuotedStr(HandleSpec(ZTable1.FieldByName('DUSUN').AsString))+','+
            QuotedStr(HandleSpec(cbPerbaikan.Text))+','+
            ZTable1.FieldByName('ID_TANGGAL').AsString+','+
            ZTable1.FieldByName('TPS').AsString+','+
            QuotedStr(ZTable1.FieldByName('STATUS1').AsString)+','+
            QuotedStr(ZTable1.FieldByName('STATUS2').AsString)+','+
            QuotedStr(ZTable1.FieldByName('GANDA').AsString)+','+
            QuotedStr(ZTable1.FieldByName('STATUS3').AsString)+','+
            QuotedStr(ZTable1.FieldByName('STATUSP').AsString)+','+
            QuotedStr(values[20])+','+
            QuotedStr(ZTable1.FieldByName('CACAT').AsString)+','+
            QuotedStr(ZTable1.FieldByName('PENDIDIKAN').AsString)+','+
            QuotedStr(ZTable1.FieldByName('PEKERJAAN').AsString)+','+
            QuotedStr(ZTable1.FieldByName('STATUSR').AsString)+','+
            QuotedStr(ZTable1.FieldByName('WN').AsString)+','+
            QuotedStr(username)+','+
            QuotedStr(kethapus)+')';
          ExecSQL;
      end;
      ZTable1.Active := False;
      ZTable1.TableName := ptabel;
      ztable1.Filter:='ID_USER='+pid;
      ztable1.filtered:=true;
      ZTable1.Active := True;
      ZTable1.First;
      if ((ZTable1.FieldByName('STATUS1').AsString='1') or (ZTable1.FieldByName('STATUS1').AsString='3')) then
        bob:=true
      else
        bob:=false;
      //query query
      with ZQuery1 do
      begin
        Active:=False;
        que:='UPDATE '+ptabel+' SET '+
        '`NIK`='+QuotedStr(values[1])+
        ',`NKK`='+QuotedStr(values[2])+
        ',`NAMA`='+QuotedStr(values[3])+
        ',`TEMPAT`='+QuotedStr(values[4])+
        ',`TANGGAL`='+QuotedStr(values[5])+
        ',`IBU`='+QuotedStr(values[26])+
        ',`AGAMA`='+QuotedStr(values[6])+
        ',`KAWIN`='+QuotedStr(values[7])+
        ',`KELAMIN`='+QuotedStr(values[8])+
        ',`KELAINAN`='+QuotedStr(values[9])+
        ',`ALAMAT`='+QuotedStr(values[10])+
        ',`RW`='+QuotedStr(values[14])+
        ',`RT`='+QuotedStr(values[15])+
        ',`DUSUN`='+QuotedStr(values[16])+
        ',`KET`='+QuotedStr(values[17])+
        ',`ID_TANGGAL`='+idtgl+
        ',`TPS`='+QuotedStr(values[18])+
        ',`STATUSP`='+QuotedStr(values[19])+
        ',`ALASANP`='+QuotedStr(values[20])+
        ',`CACAT`='+QuotedStr(values[21])+
        ',`PENDIDIKAN`='+QuotedStr(values[22])+
        ',`PEKERJAAN`='+QuotedStr(values[23])+
        ',`STATUSR`='+QuotedStr(values[24])+
        ',`STATUS2`='+QuotedStr(statushapus)+
        ',`WN`='+QuotedStr(values[25]);
        //if (bob=true) then
          que:=que+' WHERE ID_USER='+pid;
        //else
          //que:=que+',`STATUS1`='+QuotedStr('2')+' WHERE ID_USER='+pid;
        SQL.Text:=que;
        ExecSQL;
      end;
      //---------------------------HISTORY
      //STATUS 2 - TANDA
      {if (cbPerbaikan.ItemIndex=0) or (cbPerbaikan.ItemIndex=2) or (cbPerbaikan.ItemIndex=9) then begin
        //meninggal,fiktif,cabut hak pilih
        if (cbPerbaikan.ItemIndex=0) then
          UbahTanda('1')
          else
        if (cbPerbaikan.ItemIndex=2) then
          UbahTanda('2')
          else
        if (cbPerbaikan.ItemIndex=9) then
          UbahTanda('3')
      end
        else}
      if (cbPerbaikan.ItemIndex=3) then begin
        //pindah alamat
        PindahAlamat;
      end
        else
      if (cbPerbaikan.ItemIndex=4) or (cbPerbaikan.ItemIndex=5) then begin
        //bukan data ganda
        BukanGanda;
      end;
      {  else
      if (cbPerbaikan.ItemIndex=11) then begin
        //hapus tanda
        HapusTanda;
      end
        else
      if (cbPerbaikan.ItemIndex=12) then begin
        //hapus pemilih
        HapusPemilih;
      end;}
      //
      ShowMessage('Data pemilih telah berhasil diubah.');
      Close;
    end
      else
    ShowMessage('Cek jangan ada yang kosong pada : Nama Lengkap, Tanggal Lahir / Usia, Alamat (Terutama RT/RW), Status Pernikahan, Jenis Kelamin');
    end
    else
    ShowMessage('Alasan Perbaikan harus diisi, silakan mengisi kembali dengan cermat');
  end;
end;

procedure TForm3.UbahTanda(td : String);
var
  query,idx:String;
  temp:TStrings;
begin
  ZQuery1.Active:=false;
    zquery1.SQL.Clear;
    query:='UPDATE '+ptabel+' SET STATUS2='+QuotedStr(td)+' WHERE ID_USER='+pid;
    zquery1.SQL.text:=query;
    zquery1.ExecSQL;
    //cek data_ganda
    with ZQuery1 do begin
      Active:=false;
      sql.Clear;
      query:='SELECT * FROM data_ganda WHERE ID_USER1='+QuotedStr(tid+'-'+pid)+' OR ID_USER2='+QuotedStr(tid+'-'+pid);
      SQL.Text:=query;
      Active:=true;
    end;
    //kalau cuma sepasang, ubah
    if ZQuery1.RecordCount=1 then begin
      if (ZQuery1.FieldByName('ID_USER1').AsString=tid+'-'+pid) then
        idx:=ZQuery1.FieldByName('ID_USER2').AsString
      else
        idx:=ZQuery1.FieldByName('ID_USER1').AsString;
       with ZQuery1 do begin
        Active:=false;
        sql.Clear;
        query:='UPDATE '+ptabel+' SET GANDA="0" WHERE ID_USER='+pid;
        SQL.Text:=query;
        ExecSQL;
        //
        Active:=false;
        sql.Clear;
        if atb=1 then
          query:='DELETE FROM data_ganda WHERE ID_USER1='+QuotedStr(tid+'-'+pid)+' OR ID_USER2='+QuotedStr(tid+'-'+pid)
        else
          query:='DELETE FROM data_ganda WHERE (ID_USER1='+QuotedStr(tid+'-'+pid)+' AND ID_USER2='+QuotedStr(tidx+'-'+pidx)+') OR (ID_USER1='+QuotedStr(tidx+'-'+pidx)+' AND ID_USER2='+QuotedStr(tid+'-'+pid)+')';
        SQL.Text:=query;
        ExecSQL;
      end;
    end;
    //
    if (idx<>'') then begin
      with ZQuery1 do begin
        Active:=false;
        sql.Clear;
        query:='SELECT * FROM data_ganda WHERE ID_USER1='+QuotedStr(idx)+' OR ID_USER2='+QuotedStr(idx);
        SQL.Text:=query;
        Active:=true;
      end;
      //kalau sudah habis, ubah
      if (ZQuery1.RecordCount<1) and (idx<>'') then begin
         with ZQuery1 do begin
          Active:=false;
          sql.Clear;
          temp := TStringList.Create; //1
          try
            ExtractStrings(['-'], [], PChar(idx), temp);
             tidx := temp[0];
             pidx := temp[1];
          finally
            temp.Free;
          end;
          query:='SELECT * FROM penyimpan WHERE ID='+tidx;
          sql.Text:=query;
          active:=True;
          ptabelx:=FieldByName('NAMA').AsString;
          //
          Active:=false;
          sql.Clear;
          query:='UPDATE '+ptabelx+' SET GANDA="0" WHERE ID_USER='+pidx;
          SQL.Text:=query;
          ExecSQL;
         end;
      end;
    end;
end;

procedure TForm3.PindahAlamat;
var
  tanggal,pidn,tidn,query_ins,query, ique:String;
begin
  if (ptabel<>'pemilih_'+sdesa) then begin
      //pindah tabel
      ZQuery1.SQL.Clear;
      ZQuery1.SQL.Add('CREATE TABLE IF NOT EXISTS `'+'pemilih_'+sdesa+'` ('+
      '`ID_USER` int(11) NOT NULL AUTO_INCREMENT,'+
      '`NIK` char(36) NOT NULL,'+
      '`NKK` char(36) NOT NULL,'+
      '`NAMA` char(50) NOT NULL,'+
      '`TEMPAT` char(36) NOT NULL,'+
      '`TANGGAL` char(10) NOT NULL,'+
      '`IBU` char(36) NOT NULL,'+
      '`AGAMA` char(36) NOT NULL,'+
      '`KAWIN` char(1) NOT NULL,'+
      '`KELAMIN` char(1) NOT NULL,'+
      '`KELAINAN` char(36) NOT NULL,'+
      '`ALAMAT` char(150) NOT NULL,'+
      '`RW` char(3) NOT NULL,'+
      '`RT` char(3) NOT NULL,'+
      '`DUSUN` char(30) NOT NULL,'+
      '`KET` char(150) NOT NULL,'+
      '`ID_TANGGAL` int(11) NOT NULL,'+
      'TPS int(11) NOT NULL,'+
      'STATUS1 char(1) NOT NULL,'+
      'STATUS2 char(1) NOT NULL,'+
      'GANDA char(1) NOT NULL,'+
      'STATUS3 char(1) NOT NULL,'+
      'STATUSP char(1) NOT NULL,'+
      'ALASANP char(1) NOT NULL,'+
      'CACAT char(1) NOT NULL,'+
      'PENDIDIKAN char(1) NOT NULL,'+
      'PEKERJAAN char(1) NOT NULL,'+
      'STATUSR char(1) NOT NULL,'+
      'WN char(1) NOT NULL,'+
      'PRIMARY KEY (`ID_USER`)'+
      ') ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;');
      ZQuery1.ExecSQL;
      //masukin
      ZQuery1.Active:=False;
      ZQuery1.sql.Clear;
      ZQuery1.sql.Text := 'INSERT IGNORE INTO penyimpan (NAMA) VALUES ("pemilih_'+sdesa+'")';
      ZQuery1.ExecSQL;
   // showmessage('1');
    with ZTable1 do begin
      active:=false;
      TableName:=ptabel;
      Filter:='ID_USER='+QuotedStr(pid);
      Filtered:=true;
      Active:=true;
      First;
    end;
    with zquery1 do begin
      //showmessage(sdesa);
        active:=false;
        sql.Clear;
//        ShowMessage(ZTable1.fieldbyname('STATUS3').asstring);
        query_ins:='INSERT INTO '+'pemilih_'+sdesa+' (NIK,NKK,NAMA,TEMPAT,TANGGAL,IBU,AGAMA,KAWIN,KELAMIN,KELAINAN,ALAMAT,RW,RT,DUSUN,KET,ID_TANGGAL,TPS,STATUS1,STATUS2,GANDA,STATUS3,STATUSP,ALASANP,CACAT,PENDIDIKAN,PEKERJAAN,STATUSR,WN) VALUES ('+
          QuotedStr(HandleSpec(ZTable1.FieldByName('NIK').AsString))+','+
          QuotedStr(HandleSpec(ZTable1.FieldByName('NKK').AsString))+','+
          QuotedStr(HandleSpec(ZTable1.FieldByName('NAMA').AsString))+','+
          QuotedStr(HandleSpec(ZTable1.FieldByName('TEMPAT').AsString))+','+
          QuotedStr(HandleSpec(ZTable1.FieldByName('TANGGAL').AsString))+','+
          QuotedStr(HandleSpec(ZTable1.FieldByName('IBU').AsString))+','+
          QuotedStr(HandleSpec(ZTable1.FieldByName('AGAMA').AsString))+','+
          QuotedStr(ZTable1.FieldByName('KAWIN').AsString)+','+
          QuotedStr(ZTable1.FieldByName('KELAMIN').AsString)+','+
          QuotedStr(HandleSpec(ZTable1.FieldByName('KELAINAN').AsString))+','+
          QuotedStr(HandleSpec(TextAlamat.Text))+','+
          QuotedStr(cekRTRW(TextRW.Text))+','+
          QuotedStr(cekRTRW(textRT.Text))+','+
          QuotedStr(HandleSpec(txtDusun.Text))+','+
          QuotedStr(ZTable1.FieldByName('KET').AsString)+','+
          ZTable1.FieldByName('ID_TANGGAL').AsString+','+
          '0,'+
          QuotedStr(ZTable1.FieldByName('STATUS1').AsString)+','+
          QuotedStr(ZTable1.FieldByName('STATUS2').AsString)+','+
          QuotedStr('0')+','+
          QuotedStr(ZTable1.FieldByName('STATUS3').AsString)+','+
          QuotedStr(ZTable1.FieldByName('STATUSP').AsString)+','+
          QuotedStr(ZTable1.FieldByName('ALASANP').AsString)+','+
          QuotedStr(ZTable1.FieldByName('CACAT').AsString)+','+
          QuotedStr(ZTable1.FieldByName('PENDIDIKAN').AsString)+','+
          QuotedStr(ZTable1.FieldByName('PEKERJAAN').AsString)+','+
          QuotedStr(ZTable1.FieldByName('STATUSR').AsString)+','+
          QuotedStr(ZTable1.FieldByName('WN').AsString)+
          ')';
          //showmessage(query_ins);
        sql.text:=query_ins;
        ExecSQL;
    end;
//    showmessage('2');
     with ZTable2 do begin
      active:=false;
      TableName:='pemilih_'+sdesa;
      SortedFields:='ID_USER';
      SortType:=stDescending;
      Active:=true;
      First;
      pidn:=FieldByName('ID_USER').AsString;
      SortedFields:='';
      //
      active:=false;
      TableName:='penyimpan';
      Filter:='NAMA='+QuotedStr('pemilih_'+sdesa);
      Filtered:=true;
      Active:=true;
      First;
      tidn:=FieldByName('ID').AsString;
    end;
    with zquery1 do begin
        //masukkin history dah?
        
        //masukin ke history
        query:='SELECT * FROM '+ptabel+' WHERE ID_USER='+pid;
//      ShowMessage(query);
        sql.Text:=query;
        Active:=true;
        first;

        iQue:='INSERT INTO `history` (ID_USER, `NIK`, `NKK`, `NAMA`, `TEMPAT`, `TANGGAL`, '+
        '`IBU`, `AGAMA`, `KAWIN`, `KELAMIN`, `KELAINAN`, `ALAMAT`, `RW`, `RT`, `DUSUN`, `KET`, '+
        '`ID_TANGGAL`, TPS, STATUS1, STATUS2, GANDA, STATUS3, STATUSP, ALASANP'+
        ',CACAT,PENDIDIKAN,PEKERJAAN,STATUSR,WN,USERNAME, KET_AKSI) VALUES '+
        '('+QuotedStr(tid+'-'+FieldByName('ID_USER').AsString)+
        ','+QuotedStr(HandleSpec(FieldByName('NIK').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('NKK').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('NAMA').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('TEMPAT').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('TANGGAL').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('IBU').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('AGAMA').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('KAWIN').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('KELAMIN').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('KELAINAN').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('ALAMAT').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('RW').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('RT').AsString))+
        ','+QuotedStr(HandleSpec(FieldByName('DUSUN').AsString))+
        ','+QuotedStr('PINDAH ALAMAT')+
        ','+FieldByName('ID_TANGGAL').AsString+
        ','+FieldByName('TPS').AsString+
        ','+QuotedStr(FieldByName('STATUS1').AsString)+
        ','+QuotedStr(FieldByName('STATUS2').AsString)+
        ','+QuotedStr(FieldByName('GANDA').AsString)+
        ','+QuotedStr(FieldByName('STATUS3').AsString)+
        ','+QuotedStr(FieldByName('STATUSP').AsString)+
        ','+QuotedStr(FieldByName('ALASANP').AsString)+
        ','+QuotedStr(FieldByName('CACAT').AsString)+
        ','+QuotedStr(FieldByName('PENDIDIKAN').AsString)+
        ','+QuotedStr(FieldByName('PEKERJAAN').AsString)+
        ','+QuotedStr(FieldByName('STATUSR').AsString)+
        ','+QuotedStr(FieldByName('WN').AsString)+
        ','+QuotedStr(username)+
        ','+QuotedStr('2')+
        ')';
        //
        active:=false;
        sql.Clear;
        //ShowMessage(ique);
        sql.Text:=ique;
        ExecSQL;

        //hapus
        active:=false;
        sql.Clear;
        sql.Text:='UPDATE '+ptabel+' SET STATUS2=4, TPS = 0 WHERE ID_USER='+QuotedStr(pid);
        ExecSQL;
        ShowMessage('Pemilih telah pindah alamat');
    end;
  end;
end;

procedure TForm3.btnBatalClick(Sender: TObject);
begin
  Close;
end;

procedure TForm3.btnTPSClick(Sender: TObject);
var
  kode,tkab,tkec,ttps:String;
  i,idx:Integer;
begin
  with ztable1 do begin
    active:=false;
    TableName:='daftar_tps';
    filter:='ID_TPS='+idtps;
    filtered:=true;
    active:=true;
    first;
  end;
  kode:=ztable1.FieldByName('KODE_DESA').AsString;
  if (kode<>'') then begin
    tkab:=Copy(kode,1,2);
    tkec:=Copy(kode,1,5);
  end
    else
  begin
    tkab:=sDesa;
    tkec:=sKec;
  end;
  with ZTable1 do
  begin
    Active := False;
    TableName := 'kabkot';
    Filtered:=False;
    Active := True;
    First;
  end;
  cbT1.Clear;
  cbT2.Clear;
  cbT3.Clear;
  cbT4.Clear;
  i:=0;
  idx:=-1;
  while (not ZTable1.Eof) do
  begin
    cbT1.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
    if (tkab=ZTable1.FieldByName('KODE').AsString) then
      idx:=i;
    ZTable1.Next;
    Inc(i);
  end;
  //ShowMessage(ikabkot);
  cbT1.ItemIndex:=idx;
  if (kode<>'') then begin
    //KEC
    with ZTable1 do
    begin
      Active := False;
      TableName := 'kecamatan';
      Filter := 'KODE_KABKOT='+QuotedStr(tkab);
      Filtered:=True;
      Active := True;
      First;
    end;
    cbT2.Clear;
    i:=0;
    idx:=-1;
    while (not ZTable1.Eof) do
    begin
      cbT2.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
      if (tkec=ZTable1.FieldByName('KODE_KECAMATAN').AsString) then
        idx:=i;
      ZTable1.Next;
      Inc(i);
    end;
    cbT2.ItemIndex:=idx;
    //DESA
    with ZTable1 do
    begin
      Active := False;
      TableName := 'desa';
      Filter := 'KODE_KECAMATAN='+QuotedStr(tkec);
      Filtered:=True;
      Active := True;
      First;
    end;
    cbT3.Clear;
    i:=0;
    idx:=-1;
    while (not ZTable1.Eof) do
    begin
      cbT3.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
      if (kode=ZTable1.FieldByName('KODE_DESA').AsString) and (kode<>'') then
        idx:=i;
      ZTable1.Next;
      Inc(i);
    end;
    cbT3.ItemIndex:=idx;
    //TPS
    with ZTable1 do
    begin
      Active := False;
      TableName := 'daftar_tps';
      Filter := 'KODE_DESA='+QuotedStr(kode);
      Filtered:=True;
      Active := True;
      First;
    end;
    cbT4.Clear;
    i:=0;
    idx:=-1;
    while (not ZTable1.Eof) do
    begin
      cbT4.Items.Add(ZTable1.FieldByName('NAMA').AsString);
      if (idtps=ZTable1.FieldByName('id_tps').AsString) and (kode<>'') then
        idx:=i;
      ZTable1.Next;
      Inc(i);
    end;
    cbT4.ItemIndex:=idx;
  end;
  GroupBox4.Visible:=true;
  GroupBox1.Enabled:=false;
  Button2.Enabled:=false;
end;

procedure TForm3.BukanGanda;
var
  query,idx:String;
  temp:TStrings;
  tanggal, idtgl:String;
begin

  //tanggal
      tanggal := FormatDateTime('yyyy-mm-dd hh:mm:ss', Now());
      with ZQuery1 do  //masukkin nih gan
        begin
          Active:=False;
          sql.Clear;
          sql.Text := 'INSERT INTO tanggal (TANGGAL) VALUES ("'+tanggal+'")';
          ExecSQL;
        end;
      with ZQuery1 do  //masukkin nih gan
        begin
          Active:=False;
          sql.Clear;
          sql.Text := 'SELECT * FROM tanggal ORDER BY ID_TANGGAL DESC';
          Active:=True;
          idtgl := FieldByName('ID_TANGGAL').AsString;  //dapat ID_TGL
        end;
      with ZQuery1 do begin
        Active:=false;
        sql.Clear;
        query:='UPDATE '+ptabel+' SET GANDA="0" WHERE ID_USER='+pid;
        SQL.Text:=query;
        ExecSQL;
      end;
  with ZQuery1 do begin
      Active:=false;
      sql.Clear;
      query:='SELECT * FROM data_ganda WHERE ID_USER1='+QuotedStr(tid+'-'+pid)+' OR ID_USER2='+QuotedStr(tid+'-'+pid);
      SQL.Text:=query;
      Active:=true;

    end;
    //kalau cuma sepasang, ubah
    if ZQuery1.RecordCount>=1 then begin
      if (ZQuery1.FieldByName('ID_USER1').AsString=tid+'-'+pid) then
        idx:=ZQuery1.FieldByName('ID_USER2').AsString
      else
        idx:=ZQuery1.FieldByName('ID_USER1').AsString;
       with ZQuery1 do begin
        Active:=false;
        sql.Clear;
        query:='UPDATE '+ptabel+' SET GANDA="0" WHERE ID_USER='+pid;
        SQL.Text:=query;
        ExecSQL;
        //
        Active:=false;
        sql.Clear;
        if atb=1 then
          query:='DELETE FROM data_ganda WHERE ID_USER1='+QuotedStr(tid+'-'+pid)+' OR ID_USER2='+QuotedStr(tid+'-'+pid)
        else
          query:='DELETE FROM data_ganda WHERE (ID_USER1='+QuotedStr(tid+'-'+pid)+' AND ID_USER2='+QuotedStr(tidx+'-'+pidx)+') OR (ID_USER1='+QuotedStr(tidx+'-'+pidx)+' AND ID_USER2='+QuotedStr(tid+'-'+pid)+')';
        SQL.Text:=query;
        ExecSQL;
      end;
    end;
    /////
    {if (idx<>'') then begin
      with ZQuery1 do begin
        Active:=false;
        sql.Clear;
        query:='SELECT * FROM data_ganda WHERE ID_USER1='+QuotedStr(idx)+' OR ID_USER2='+QuotedStr(idx);
        SQL.Text:=query;
        Active:=true;
      end;
      //kalau sudah habis, ubah
      if (ZQuery1.RecordCount<1) and (idx<>'') then begin
         with ZQuery1 do begin
          Active:=false;
          sql.Clear;
          temp := TStringList.Create; //1
          try
            ExtractStrings(['-'], [], PChar(idx), temp);
             tidx := temp[0];
             pidx := temp[1];
          finally
            temp.Free;
          end;
          query:='SELECT * FROM penyimpan WHERE ID='+tidx;
          sql.Text:=query;
          active:=True;
          ptabelx:=FieldByName('NAMA').AsString;
          //
          active:=false;
          query:='SHOW TABLES LIKE '+quotedStr(ptabelx);
          SQL.Text:=query;
          active:=true;
          if(recordCount > 0) then begin
            Active:=false;
            sql.Clear;
            query:='UPDATE '+ptabelx+' SET GANDA="0", ID_TANGGAL = '+idtgl+' WHERE ID_USER='+pidx;
            SQL.Text:=query;
            ExecSQL;
          end;
         end;
      end;
    end;}
end;

procedure TForm3.HapusTanda;
var
  buttonSelected : Integer;
  que:string;
begin
  buttonSelected := MessageDlg('Apakah anda yakin akan menghilangkan tanda pemilih ini?',mtConfirmation , mbOKCancel, 0);
  if buttonSelected = mrOK  then begin
    with ZQuery1 do begin
      active:=false;
      sql.Clear;
      que:='UPDATE '+ptabel+' SET STATUS2="0" WHERE ID_USER='+pid;
      sql.Text:=que;
      ExecSQL;
    end;
    ShowMessage('Tanda telah dihilangkan');
  end;
end;

procedure TForm3.HapusPemilih;
var
  query,idx,ique,itabel:String;
  temp:TStrings;
begin
  with ZQuery1 do begin
      //masukkin history?
      active:=false;
      sql.Clear;
      itabel:=ptabel+'-'+pid;
      query:='UPDATE '+ptabel+' SET STATUS2='+QuotedStr('5')+' WHERE ID_USER='+pid;
//      ShowMessage(query);
      sql.Text:=query;
      ExecSQL;
    end;
    with ZQuery1 do begin
      Active:=false;
      sql.Clear;
      query:='SELECT * FROM data_ganda WHERE ID_USER1='+QuotedStr(tid+'-'+pid)+' OR ID_USER2='+QuotedStr(tid+'-'+pid);
      SQL.Text:=query;
      Active:=true;
    end;
    //kalau cuma sepasang, ubah
    if ZQuery1.RecordCount=1 then begin
      if (ZQuery1.FieldByName('ID_USER1').AsString=tid+'-'+pid) then
        idx:=ZQuery1.FieldByName('ID_USER2').AsString
      else
        idx:=ZQuery1.FieldByName('ID_USER1').AsString;
       with ZQuery1 do begin
        Active:=false;
        sql.Clear;
        query:='UPDATE '+ptabel+' SET GANDA="0" WHERE ID_USER='+pid;
        SQL.Text:=query;
        ExecSQL;
        //
        Active:=false;
        sql.Clear;
        if atb=1 then
          query:='DELETE FROM data_ganda WHERE ID_USER1='+QuotedStr(tid+'-'+pid)+' OR ID_USER2='+QuotedStr(tid+'-'+pid)
        else
          query:='DELETE FROM data_ganda WHERE (ID_USER1='+QuotedStr(tid+'-'+pid)+' AND ID_USER2='+QuotedStr(tidx+'-'+pidx)+') OR (ID_USER1='+QuotedStr(tidx+'-'+pidx)+' AND ID_USER2='+QuotedStr(tid+'-'+pid)+')';
        SQL.Text:=query;
        ExecSQL;
      end;
    end;
    //
    if (idx<>'') then begin
      with ZQuery1 do begin
        Active:=false;
        sql.Clear;
        query:='SELECT * FROM data_ganda WHERE ID_USER1='+QuotedStr(idx)+' OR ID_USER2='+QuotedStr(idx);
        SQL.Text:=query;
        Active:=true;
      end;
      //kalau sudah habis, ubah
      if (ZQuery1.RecordCount<1) and (idx<>'') then begin
         with ZQuery1 do begin
          Active:=false;
          sql.Clear;
          temp := TStringList.Create; //1
          try
            ExtractStrings(['-'], [], PChar(idx), temp);
             tidx := temp[0];
             pidx := temp[1];
          finally
            temp.Free;
          end;
          query:='SELECT * FROM penyimpan WHERE ID='+tidx;
          sql.Text:=query;
          active:=True;
          ptabelx:=FieldByName('NAMA').AsString;
          //
          Active:=false;
          sql.Clear;
          query:='UPDATE '+ptabelx+' SET GANDA="0" WHERE ID_USER='+pidx;
          SQL.Text:=query;
          ExecSQL;
         end;
      end;
    end;
end;

procedure TForm3.Button2Click(Sender: TObject);
begin
  idTPS:=idTPS2;
  //
  cbKabkot.Items:=cbT1.Items;
  cbKabkot.ItemIndex:=cbT1.ItemIndex;
  cbKec.Items:=cbT2.Items;
  cbKec.ItemIndex:=cbT2.ItemIndex;
  cbDesa.Items:=cbT3.Items;
  cbDesa.ItemIndex:=cbT3.ItemIndex;
  txtTPS.Text:=cbT4.Items[cbT4.ItemIndex];
  //
  GroupBox4.Visible:=false;
  GroupBox1.Enabled:=true;
end;

procedure TForm3.Button3Click(Sender: TObject);
begin
  GroupBox4.Visible:=false;
  GroupBox1.Enabled:=true;
end;

constructor TForm3.Create(AOwner: TComponent; tabel: String; ID:String; IDT:String; isganda:Boolean; bta:Integer; kodelog:String; user:String=''; tabelx: String=''; IDX:String=''; IDTX:String='');
begin
    username:=user;
    ptabel:=tabel;
    pid:=ID;
    tid:=IDT;
    ganda:=isganda;
    atb:=bta;
    ptabelx:=tabelx;
    pidx:=IDX;
    tidx:=IDTX;

    if(length(kodelog)>2)then
    begin
      skodekab:=copy(kodelog,1,2);
      skodekec:=kodelog;
    end
    else
    begin
      skodekab:=kodelog;
      skodekec:='';
    end;
    //skode:=kodelog;

    inherited Create(AOwner);
end;

procedure TForm3.KeyOnlyNum(Sender: TObject; var Key: Char);
begin
  if not(Key in [#8, '0' .. '9']) then
  begin
    Key := #0;
  end;
end;

procedure TForm3.TextNamaKeyPress(Sender: TObject; var Key: Char);
begin
  if not(Key in [#8, #32, #44 .. #46, #65 .. #90, #97 .. #122, '0' .. '9']) then
  begin
    Key := #0;
  end;
end;

procedure TForm3.TextNIKKeyPress(Sender: TObject; var Key: Char);
begin
  KeyOnlyNum(Sender, Key);
end;

procedure TForm3.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TForm3.FormCreate(Sender: TObject);
var
  idx,i,crow:Integer;
  sTgl:TStrings;
  tgl,d,m,y,que:String;
  tblname,kkota,kkec,kdesa:String;
  setting:TSetting_Form;
begin
//9showMessage(username);
  setting:=TSetting_Form.Create;
  ZConnection1.HostName:=setting.host;
  ZConnection1.Protocol:=setting.protocol;
  ZConnection1.Database:=setting.database;
  ZConnection1.User:=setting.user;
  ZConnection1.Password:=setting.password;
  ZConnection1.Connected:=true;
  ztable1.Connection:=ZConnection1;
  ztable2.Connection:=ZConnection1;
  ZQuery1.Connection:=ZConnection1;
  BorderIcons := BorderIcons - [biMaximize];
  //dummy
  {ptabel:='pemilih_09110005';
  pid:='3';}
  //dummy
  WindowState:=wsNormal;
  ikabkot:=Copy(ptabel,9,2);
  ikec:=Copy(ptabel,9,5);
  idesa:=Copy(ptabel,9,8);
  //TABEL-TABEL
  with ZTable1 do begin
    Active:=False;
    TableName:='penyimpan';
    Active:=True;
    SetLength(bPenyimpan,RecordCount);
    I:=0;
    while (not Eof) do begin
      bPenyimpan[I].ID:=FieldByName('ID').AsString;
      bPenyimpan[I].NAMA:=FieldByName('NAMA').AsString;
      Inc(I);
      Next;
    end;
  end;
  //desa
  with ZTable1 do begin
    Active:=False;
    TableName:='desa';
    Active:=True;
    SetLength(bDesa,RecordCount);
    I:=0;
    while (not Eof) do begin
      bDesa[I].KODE:=FieldByName('KODE_DESA').AsString;
      bDesa[I].NAMA:=FieldByName('TAMPIL').AsString;
      Inc(I);
      Next;
    end;
  end;
  //kec
  with ZTable1 do begin
    Active:=False;
    TableName:='kecamatan';
    Active:=True;
    SetLength(bKec,RecordCount);
    I:=0;
    while (not Eof) do begin
      bKec[I].KODE:=FieldByName('KODE_KECAMATAN').AsString;
      bKec[I].NAMA:=FieldByName('TAMPIL').AsString;
      Inc(I);
      Next;
    end;
  end;
  //kota
  with ZTable1 do begin
    Active:=False;
    TableName:='kabkot';
    Active:=True;
    SetLength(bKabkot,RecordCount);
    I:=0;
    while (not Eof) do begin
      bKabkot[I].KODE:=FieldByName('KODE').AsString;
      bKabkot[I].NAMA:=FieldByName('TAMPIL').AsString;
      Inc(I);
      Next;
    end;
  end;
  //------------------------------------------------
  with cbKawin do
  begin
    Clear;
    Items.Add('Belum');
    Items.Add('Sudah');
    Items.Add('Pernah');
  end;
  with cbJk do
  begin
    Clear;
    Items.Add('Laki');
    Items.Add('Perempuan');
  end;
  with ZTable1 do
  begin
    Active:=False;
    TableName:=ptabel;
    Filter:='ID_USER = '+pid;
    Filtered:=True;
    Active:=True;
    First;
  end;
  itps := ZTable1.FieldByName('TPS').AsString;
  TextNIK.Text := ZTable1.FieldByName('NIK').AsString;
  TextKK.Text := ZTable1.FieldByName('NKK').AsString;
  TextNama.Text := ZTable1.FieldByName('NAMA').AsString;
  TextTempat.Text := ZTable1.FieldByName('TEMPAT').AsString;
  TextIbu.Text := ZTable1.FieldByName('IBU').AsString;
  cbTgl.Clear;
  cbTgl.items.Add(' ');
  for I := 1 to 31 do
    cbTgl.Items.Add(IntToStr(I));
  cbBulan.Clear;
  cbBulan.items.Add(' ');
  for I := 1 to 12 do
    cbBulan.Items.Add(IntToStr(I));
  tgl:=ZTable1.FieldByName('TANGGAL').AsString;
  sTgl := TStringList.Create;
    try
      ExtractStrings(['-'], [], PChar(tgl), sTgl);
      if(sTgl.count=3) then begin
        d := sTgl[0];
        m := sTgl[1];
        y := sTgl[2];
      end
      else begin
        d := '01';
        m := '01';
        y := '1900';
      end;
    finally
      sTgl.Free;
    end;
//  d:=Copy(tgl,1,2);
//  m:=Copy(tgl,4,2);
//  y:=Copy(tgl,7,4);
  cbTgl.ItemIndex:=StrToInt(d);
  cbBulan.ItemIndex:=StrToInt(m);
  txtTahun.Text:=y;
//  TextIbu.Text := ZTable1.FieldByName('IBU').AsString;
  TextAgama.Text := ZTable1.FieldByName('AGAMA').AsString;
  if (ZTable1.FieldByName('KAWIN').AsString='B') then
    cbKawin.ItemIndex:=0
  else if (ZTable1.FieldByName('KAWIN').AsString='S') then
    cbKawin.ItemIndex:=1
  else
    cbKawin.ItemIndex:=2;
  if (ZTable1.FieldByName('KELAMIN').AsString='L') then
    cbJk.ItemIndex:=0
  else
    cbJk.ItemIndex:=1;
//  TextKelainan.Text := ZTable1.FieldByName('KELAINAN').AsString;
  TextAlamat.Text := ZTable1.FieldByName('ALAMAT').AsString;
  TextRW.Text := ZTable1.FieldByName('RW').AsString;
  TextRT.Text := ZTable1.FieldByName('RT').AsString;
  TextKeterangan.Text := ZTable1.FieldByName('KET').AsString;
  txtDusun.Text := ZTable1.FieldByName('DUSUN').AsString;
  //TPS
  txtTPS.Enabled:=false;
  with ZTable2 do begin
    active:=false;
    TableName:='daftar_tps';
    filter:='ID_TPS='+IntToStr(ZTable1.FieldByName('TPS').AsInteger);
    idtps:=ZTable1.FieldByName('TPS').AsString;
    filtered:=true;
    active:=true;
    first;
  end;
  txtTPS.Text:=ZTable2.FieldByName('NAMA').AsString;
  //STATUSP
  with cbPemilih do
  begin
    Clear;
    Items.Add('Biasa');
    Items.Add('Cacat Mental/Jiwa');
    Items.Add('Dicabut Hak Pilih');
    Items.Add('Pidana 5 Tahun');
    Items.Add('Tidak Punya Hak Pilih');
    Items.Add('Belum Punya Hak Pilih');
  end;
  case ZTable1.FieldByName('STATUSP').AsInteger of
    1 : cbPemilih.ItemIndex:=0;
    2 : cbPemilih.ItemIndex:=1;
    3 : cbPemilih.ItemIndex:=2;
    4 : cbPemilih.ItemIndex:=3;
    5 : cbPemilih.ItemIndex:=4;
    6 : cbPemilih.ItemIndex:=5;
    else
  end;
  //ALASANP
  cbPerbaikan.Clear;
//  cbPerbaikan.Items.Add('Meninggal Dunia'); //ini ilang
//  cbPerbaikan.Items.Add('Pindah domisili'); //ini ilang
//  cbPerbaikan.Items.Add('Tidak dikenal'); //ini ilang
  cbPerbaikan.Items.Add('Salah status pemilih');
  cbPerbaikan.Items.Add('Salah jenis kelamin');
  cbPerbaikan.Items.Add('Salah nama');
  cbPerbaikan.Items.Add('Salah alamat');
//  cbPerbaikan.Items.Add('Terdaftar ganda'); //ini ilang
//  cbPerbaikan.Items.Add('Dicabut Hak Pilih'); //ini ilang
  cbPerbaikan.Items.Add('Bukan Data Ganda'); //ini
//  cbPerbaikan.Items.Add('Hapus Tanda'); //ini ilang
//  cbPerbaikan.Items.Add('Hapus Pemilih'); //ini ilang
  cbPerbaikan.Items.Add('Memang Ganda Tetapi Memilih di Kel/Desa Ini'); //ini
  cbPerbaikan.Items.Add('Lainnya');
  //CACAT
  with cbCacat do
  begin
    Clear;
    Items.Add('Tanpa Cacat');
    Items.Add('Cacat Fisik');
    Items.Add('Cacat Netra/buta');
    Items.Add('Cacat rungu wicara');
    Items.Add('Cacat mental jiwa');
    Items.Add('Cacat fisik/mental');
    Items.Add('Cacat Lainnya');
  end;
  case ZTable1.FieldByName('CACAT').AsInteger of
    1 : cbCacat.ItemIndex:=1;
    2 : cbCacat.ItemIndex:=2;
    3 : cbCacat.ItemIndex:=3;
    4 : cbCacat.ItemIndex:=4;
    5 : cbCacat.ItemIndex:=5;
    6 : cbCacat.ItemIndex:=6;
    else
    cbCacat.ItemIndex:=0;
  end;
  //PENDIDIKAN
  with cbPend do
  begin
    Clear;
    Items.Add('Tidak/Belum Sekolah');
    Items.Add('Tamat SD');
    Items.Add('Tidak Tamat SD');
    Items.Add('SLTP');
    Items.Add('SLTA');
    Items.Add('DI/II');
    Items.Add('DIII');
    Items.Add('S1/D4');
    Items.Add('S2');
    Items.Add('S3');
  end;
  case ZTable1.FieldByName('PENDIDIKAN').AsInteger of
    1 : cbPend.ItemIndex:=0;
    2 : cbPend.ItemIndex:=1;
    3 : cbPend.ItemIndex:=2;
    4 : cbPend.ItemIndex:=3;
    5 : cbPend.ItemIndex:=4;
    6 : cbPend.ItemIndex:=5;
    7 : cbPend.ItemIndex:=6;
    8 : cbPend.ItemIndex:=7;
    9 : cbPend.ItemIndex:=8;
    10 : cbPend.ItemIndex:=9;
    else
  end;
  //PEKERJAAN
  with cbPek do
  begin
    Clear;
    Items.Add('Pelajar');
    Items.Add('Pengurus RT');
    Items.Add('Pensiunan');
    Items.Add('Blm/tdk bekerja');
    Items.Add('Petani');
    Items.Add('Peternak');
    Items.Add('Nelayan');
    Items.Add('Industri');
    Items.Add('Kontruksi');
    Items.Add('Perdagangan');
    Items.Add('Transportasi');
    Items.Add('PNS');
    Items.Add('TNI');
    Items.Add('Polri');
    Items.Add('Pensiunan TNI/Polri');
    Items.Add('Jasa lainnya');
  end;
  case ZTable1.FieldByName('PEKERJAAN').AsInteger of
    1 : cbPek.ItemIndex:=0;
    2 : cbPek.ItemIndex:=1;
    3 : cbPek.ItemIndex:=2;
    4 : cbPek.ItemIndex:=3;
    5 : cbPek.ItemIndex:=4;
    6 : cbPek.ItemIndex:=5;
    7 : cbPek.ItemIndex:=6;
    8 : cbPek.ItemIndex:=7;
    9 : cbPek.ItemIndex:=8;
    10 : cbPek.ItemIndex:=9;
    11 : cbPek.ItemIndex:=10;
    12 : cbPek.ItemIndex:=11;
    13 : cbPek.ItemIndex:=12;
    14 : cbPek.ItemIndex:=13;
    15 : cbPek.ItemIndex:=14;
    16 : cbPek.ItemIndex:=15;
    else
  end;
  //STATUSR
  with cbStatR do
  begin
    Clear;
    Items.Add('Kepala RT');
    Items.Add('Istri/Suami');
    Items.Add('Anak');
    Items.Add('Menantu');
    Items.Add('Cucu');
    Items.Add('Orang Tua');
    Items.Add('Mertua');
    Items.Add('Famili Lain');
    Items.Add('Pembantu');
    Items.Add('Lainnya');
  end;
  case ZTable1.FieldByName('STATUSR').AsInteger of
    1 : cbStatR.ItemIndex:=0;
    2 : cbStatR.ItemIndex:=1;
    3 : cbStatR.ItemIndex:=2;
    4 : cbStatR.ItemIndex:=3;
    5 : cbStatR.ItemIndex:=4;
    6 : cbStatR.ItemIndex:=5;
    7 : cbStatR.ItemIndex:=6;
    8 : cbStatR.ItemIndex:=7;
    9 : cbStatR.ItemIndex:=8;
    10 : cbStatR.ItemIndex:=9;
    else
  end;
  //WN
  with cbWN do
  begin
    Clear;
    Items.Add('WNI');
    Items.Add('WNA');
  end;
  case ZTable1.FieldByName('WN').AsInteger of
    1 : cbWN.ItemIndex:=0;
    2 : cbWN.ItemIndex:=1;
    else
  end;
  //KOTA
  with ZTable1 do
  begin
    Active := False;
    TableName := 'kabkot';
    Filtered:=False;
    Active := True;
    First;
  end;
  cbKabkot.Clear;
  i:=0;
  while (not ZTable1.Eof) do
  begin
    cbKabkot.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
    if (ikabkot=ZTable1.FieldByName('KODE').AsString) then begin
      idx:=i;
      sKabkot:=ZTable1.FieldByName('KODE').AsString;
    end;
    ZTable1.Next;
    Inc(i);
  end;
  if (skodekab<>'00') then cbKabkot.Enabled:=false;
  //ShowMessage(ikabkot);
  cbKabkot.ItemIndex:=idx;
  //KEC
  with ZTable1 do
  begin
    Active := False;
    TableName := 'kecamatan';
    Filter := 'KODE_KABKOT='+QuotedStr(ikabkot);
    Filtered:=True;
    Active := True;
    First;
  end;
  cbKec.Clear;
  i:=0;
  while (not ZTable1.Eof) do
  begin
    cbKec.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
    if (ikec=ZTable1.FieldByName('KODE_KECAMATAN').AsString) then begin
      idx:=i;
      skec:=ZTable1.FieldByName('KODE_KECAMATAN').AsString;
    end;
    ZTable1.Next;
    Inc(i);
  end;
  cbKec.ItemIndex:=idx;

  //DESA
  with ZTable1 do
  begin
    Active := False;
    TableName := 'desa';
    Filter := 'KODE_KECAMATAN='+QuotedStr(ikec);
    Filtered:=True;
    Active := True;
    First;
  end;
  cbDesa.Clear;
  i:=0;
  while (not ZTable1.Eof) do
  begin
    cbDesa.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
    if (idesa=ZTable1.FieldByName('KODE_DESA').AsString) then begin
      idx:=i;
      //showmessage(inttostr(i));
      sdesa:=ZTable1.FieldByName('KODE_DESA').AsString;
    end;
    ZTable1.Next;
    Inc(i);
  end;
  cbDesa.ItemIndex:=idx;
  if (skodekec<>'') then cbkec.Enabled:=false;

  //TPS
  stps:= '0';
  with ZTable1 do
  begin
    Active := False;
    TableName := 'daftar_tps';
    Filter := 'KODE_DESA='+QuotedStr(idesa);
    Filtered:=True;
    Active := True;
    First;
  end;
  cbTPS.Clear;
  cbTPS.Items.Add('');
  i:=1;
  idx:=0;
  while (not ZTable1.Eof) do
  begin
    cbTPS.Items.Add(ZTable1.FieldByName('NAMA').AsString);
    if (itps=ZTable1.FieldByName('ID_TPS').AsString) then begin
      idx:=i;
      stps:=ZTable1.FieldByName('ID_TPS').AsString;
      //showmessage(inttostr(i));
      //showmessage(inttostr(idx));
      //sdesa:=ZTable1.FieldByName('KODE_DESA').AsString;
    end;
    ZTable1.Next;
    Inc(i);
  end;
  cbTPS.ItemIndex:=idx;

  //sg
  sgHistory.ColCount:=30;
  sgHistory.Cells[0,0]:='TANGGAL';
  sgHistory.Cells[1,0]:='NIK';
  sgHistory.Cells[2,0]:='NKK';
  sgHistory.Cells[3,0]:='Nama';
  sgHistory.Cells[4,0]:='Tempat Lahir';
  sgHistory.Cells[5,0]:='Tanggal Lahir';
  sgHistory.Cells[6,0]:='Nama Ibu';
  sgHistory.Cells[7,0]:='Agama';
  sgHistory.Cells[8,0]:='Perkawinan';
  sgHistory.Cells[9,0]:='Jenis Kelamin';
  sgHistory.Cells[10,0]:='Kelainan';
  sgHistory.Cells[11,0]:='Alamat';
  sgHistory.Cells[12,0]:='Kota/Kab';
  sgHistory.Cells[13,0]:='Kecamatan';
  sgHistory.Cells[14,0]:='Desa/Kel';
  sgHistory.Cells[15,0]:='RW';
  sgHistory.Cells[16,0]:='RT';
  sgHistory.Cells[17,0]:='Dusun';
  //
  sgHistory.Cells[18,0]:='TPS';
  sgHistory.Cells[19,0]:='STATUS1';
  sgHistory.Cells[20,0]:='STATUS2';
  sgHistory.Cells[21,0]:='GANDA';
  sgHistory.Cells[22,0]:='Status Pemilih';
  sgHistory.Cells[29,0]:='Alasan Perbaikan';
  sgHistory.Cells[24,0]:='Penyandang Cacat';
  sgHistory.Cells[25,0]:='Pendidikan';
  sgHistory.Cells[26,0]:='Pekerjaan';
  sgHistory.Cells[27,0]:='Status Rumah Tangga';
  sgHistory.Cells[28,0]:='Warga Negara';
  sgHistory.Cells[23,0]:='Keterangan';
  crow:=2;
  sgHistory.RowCount:=crow;
  daftarHistori:=tStringList.Create;
  with zquery1 do begin
    active:=false;
    sql.Clear;
    que:='SELECT * FROM history WHERE ID_USER='+QuotedStr(tid+'-'+pid)+' ORDER BY TANGGAL_AKSI DESC';
//    ShowMessage(que);
    sql.Text:=que;
    Active:=true;
    First;
    tblName:=getSimpan(tid);
    kKota:=Copy(tblName,9,2);
    kKota:=getKabkot(kKota);
    kKec:=Copy(tblName,9,5);
    kKec:=getKec(kKec);
    kDesa:=Copy(tblName,9,8);
    kDesa:=getDesa(kDesa);
    while (not Eof) do begin
      if (FieldByName('ID_HISTORY').AsString<>'') then begin
        daftarHistori.add(FieldByName('ID_HISTORY').AsString);
        sgHistory.Cells[0,cRow-1]:=FieldByName('TANGGAL_AKSI').AsString;
        sgHistory.Cells[1,cRow-1]:=FieldByName('NIK').AsString;
        sgHistory.Cells[2,cRow-1]:=FieldByName('NKK').AsString;
        sgHistory.Cells[3,cRow-1]:=FieldByName('NAMA').AsString;
        sgHistory.Cells[4,cRow-1]:=FieldByName('TEMPAT').AsString;
        sgHistory.Cells[5,cRow-1]:=FieldByName('TANGGAL').AsString;
        sgHistory.Cells[6,cRow-1]:=FieldByName('IBU').AsString;
        sgHistory.Cells[7,cRow-1]:=FieldByName('AGAMA').AsString;
        sgHistory.Cells[8,cRow-1]:=FieldByName('KAWIN').AsString;
        sgHistory.Cells[9,cRow-1]:=FieldByName('KELAMIN').AsString;
        sgHistory.Cells[10,cRow-1]:=FieldByName('KELAINAN').AsString;
        sgHistory.Cells[11,cRow-1]:=FieldByName('ALAMAT').AsString;
        sgHistory.Cells[12,cRow-1]:=kkota;
        sgHistory.Cells[13,cRow-1]:=kkec;
        sgHistory.Cells[14,cRow-1]:=kdesa;
        sgHistory.Cells[15,cRow-1]:=FieldByName('RW').AsString;
        sgHistory.Cells[16,cRow-1]:=FieldByName('RT').AsString;
        sgHistory.Cells[17,cRow-1]:=FieldByName('DUSUN').AsString;
        //
        sgHistory.Cells[18,cRow-1]:=ketTPS(FieldByName('TPS').AsString);
        sgHistory.Cells[19,cRow-1]:=kets1(FieldByName('STATUS1').AsInteger);
        sgHistory.Cells[20,cRow-1]:=kets2(FieldByName('STATUS2').AsInteger);
        sgHistory.Cells[21,cRow-1]:=ketGanda(FieldByName('GANDA').AsInteger);
        sgHistory.Cells[22,cRow-1]:=ketsp(FieldByName('STATUSP').AsInteger);
        sgHistory.Cells[29,cRow-1]:=ketap(FieldByName('ALASANP').AsInteger);
        sgHistory.Cells[24,cRow-1]:=ketca(FieldByName('CACAT').AsInteger);
        sgHistory.Cells[25,cRow-1]:=ketpend(FieldByName('PENDIDIKAN').AsInteger);
        sgHistory.Cells[26,cRow-1]:=ketpek(FieldByName('PEKERJAAN').AsInteger);
        sgHistory.Cells[27,cRow-1]:=ketsr(FieldByName('STATUSR').AsInteger);
        sgHistory.Cells[28,cRow-1]:=ketwn(FieldByName('WN').AsInteger);
        sgHistory.Cells[23,cRow-1]:=FieldByName('KET').AsString;
        Inc(crow);
        sgHistory.RowCount:=crow;
      end;
      Next;
    end;
  end;
  for i := 0 to sgHistory.ColCount - 1 do
    AutoSizeCol(sgHistory, i);
end;

function TForm3.ketTPS(id:String):String;
begin
  with ZTable1 do begin
    active:=false;
    tablename:='daftar_tps';
    filter:='ID_TPS='+id;
    filtered:=true;
    active:=true;
    First;
    Result:=FieldByName('NAMA').AsString;
  end;
end;

function TForm3.ketS1(id:integer):String;
begin
  case id of
    0 : Result:='DPS';
    1 : Result:='DPS Baru';
    2 : Result:='DPS Edit';
    else
      Result:='';
    end;
end;

function TForm3.ketS2(id:integer):String;
begin
  case id of
    1 : Result:='Meninggal Dunia';
    2 : Result:='Data Fiktif';
    3 : Result:='TNI/POLRI';
    4 : Result:='Pindah Alamat';
    5 : Result:='HAPUS';
    else
      Result:='';
    end;
end;

function TForm3.ketGanda(id:integer):String;
begin
  if id=0 then
    Result:='TIDAK'
  else
    Result:='IYA';
end;

function TForm3.ketSP(id:integer):String;
begin
  case id of
    1 : Result:='Biasa';
    2 : Result:='Cacat Mental/Jiwa';
    3 : Result:='Dicabut Hak Pilih';
    4 : Result:='Pidana 5 Tahun';
    5 : Result:='Tidak Punya Hak Pilih';
    6 : Result:='Belum Punya Hak Pilih';
    else
      Result:='';
    end;
end;

function TForm3.ketAP(id:integer):String;
begin
  case id of
    //1 : Result:='Meninggal Dunia';
    //2 : Result:='Pindah domisili';
    //3 : Result:='Tidak dikenal';
    1 : Result:='Salah status pemilih';
    2 : Result:='Salah jenis kelamin';
    3 : Result:='Salah nama';
    4 : Result:='Salah alamat';
    //8 : Result:='Terdaftar ganda';
    //10 : Result:='Dicabut Hak Pilih';
    5 : Result:='Bukan Data Ganda';
    //12 : Result:='Hapus Tanda';
    //13 : Result:='Hapus Pemilih';
    6 : Result :='Memang Ganda Tetapi Memilih di Kel/Desa Ini';
    7 : Result:='Lainnya';
    else
      Result:='';
    end;
end;

function TForm3.ketCa(id:integer):String;
begin
  case id of
    1 : Result:='Cacat Fisik';
    2 : Result:='Cacat Netra/buta';
    3 : Result:='Cacat rungu wicara';
    4 : Result:='Cacat mental jiwa';
    5 : Result:='Cacat fisik/mental';
    6 : Result:='Cacat lainnya';
    else
      Result:='';
    end;
end;

function TForm3.ketPend(id:integer):String;
begin
  case id of
    1 : Result:='Tidak/Belum Sekolah';
    3 : Result:='Tidak Tamat SD';
    4 : Result:='SLTP';
    5 : Result:='SLTA';
    6 : Result:='DI/II';
    7 : Result:='DIII';
    8 : Result:='S1/D4';
    9 : Result:='S2';
    10 : Result:='S3';
    2 : Result:='Tamat SD';
    else
      Result:='';
  end;
end;

function TForm3.ketSR(id:integer):String;
begin
  case id of
  1 : Result:='Kepala RT';
  2 : Result:='Istri/Suami';
  3 : Result:='Anak';
  4 : Result:='Menantu';
  5 : Result:='Cucu';
  6 : Result:='Orang Tua';
  7 : Result:='Mertua';
  8 : Result:='Famili Lain';
  9 : Result:='Pembantu';
  10 : Result:='Lainnya';
  else
    Result:='';
  end;
end;

function TForm3.ketPek(id:integer):String;
begin
  case id of
    1 : Result:='Pelajar';
    2 : Result:='Pengurus RT';
    3 : Result:='Pensiunan';
    4 : Result:='Blm/tdk bekerja';
    5 : Result:='Petani';
    6 : Result:='Peternak';
    7 : Result:='Nelayan';
    8 : Result:='Industri';
    9 : Result:='Kontruksi';
    10 : Result:='Perdagangan';
    11 : Result:='Transportasi';
    12 : Result:='PNS';
    13 : Result:='TNI';
    14 : Result:='Polri';
    15 : Result:='Jasa lainnya';
    else
      Result:='';
  end;
end;

function TForm3.ketWN(id:integer):String;
begin
  case id of
    1 : Result:='WNI';
    2 : Result:='WNA';
    else
      Result:=''
  end;
end;

function TForm3.getKec(kode:String):String;
var
  I:integer;
begin
  for I := 0 to Length(bKec) - 1 do begin
    if (bKec[i].KODE=kode) then
      break;
  end;
    Result:=bKec[i].NAMA;
end;

function TForm3.getKabkot(kode:String):String;
var
  I:integer;
begin
  for I := 0 to Length(bKabkot) - 1 do begin
    if (bKabkot[i].KODE=kode) then
      break;
  end;
    Result:=bKabkot[i].NAMA;
end;

function TForm3.getIdsimpan(tabel:String):String;
var
  I:integer;
  res:String;
begin
  res:='';
  for I := 0 to Length(bPenyimpan) - 1 do begin
    if (bPenyimpan[i].NAMA=tabel) then begin
      res:=bPenyimpan[i].ID;
      break;
    end;
  end;
    Result:=res;
end;

function TForm3.getSimpan(id:String):String;
var
  I:integer;
  res:String;
begin
  res:='';
  for I := 0 to Length(bPenyimpan) - 1 do begin
    if (bPenyimpan[i].ID=id) then begin
      res:=bPenyimpan[i].NAMA;
      break;
    end;
  end;
    Result:=res;
end;

function TForm3.getTabel(id:String):String;
begin
  Result:=Copy(id,1,16);
end;

function TForm3.getIduser(id:String):String;
begin
  Result:=Copy(id,17,(Length(id)-16));
end;

function TForm3.getDesa(kode:String):String;
var
  I:integer;
begin
  for I := 0 to Length(bDesa) - 1 do begin
    if (bDesa[i].KODE=kode) then
      break;
  end;
    Result:=bDesa[i].NAMA;
end;

procedure TForm3.AutoSizeCol(Grid: TStringGrid; Column: integer);
var
  i, W, WMax: integer;
begin
  WMax := 0;
  for i := 0 to (Grid.RowCount - 1) do begin
    W := Grid.Canvas.TextWidth(Grid.Cells[Column, i]);
    if W > WMax then
      WMax := W;
  end;
  Grid.ColWidths[Column] := WMax + 5;
end;

procedure TForm3.Button4Click(Sender: TObject);
var
kode,tkab,tkec,ttps:String;
i,idx:Integer;
begin
with ztable1 do begin
active:=false;
TableName:='daftar_tps';
filter:='ID_TPS='+idtps;
filtered:=true;
active:=true;
first;
end;
kode:=ztable1.FieldByName('KODE_DESA').AsString;
if (kode<>'') then begin
tkab:=Copy(kode,1,2);
tkec:=Copy(kode,1,5);
end
else
begin
tkab:=sDesa;
tkec:=sKec;
end;
with ZTable1 do
begin
Active := False;
TableName := 'kabkot';
Filtered:=False;
Active := True;
First;
end;
cbT1.Clear;
cbT2.Clear;
cbT3.Clear;
cbT4.Clear;
i:=0;
idx:=-1;
while (not ZTable1.Eof) do
begin
cbT1.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
if (tkab=ZTable1.FieldByName('KODE').AsString) then
idx:=i;
ZTable1.Next;
Inc(i);
end;
//ShowMessage(ikabkot);
cbT1.ItemIndex:=idx;
if (kode<>'') then begin
//KEC
with ZTable1 do
begin
Active := False;
TableName := 'kecamatan';
Filter := 'KODE_KABKOT='+QuotedStr(tkab);
Filtered:=True;
Active := True;
First;
end;
cbT2.Clear;
i:=0;
idx:=-1;
while (not ZTable1.Eof) do
begin
cbT2.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
if (tkec=ZTable1.FieldByName('KODE_KECAMATAN').AsString) then
idx:=i;
ZTable1.Next;
Inc(i);
end;
cbT2.ItemIndex:=idx;
//DESA
with ZTable1 do
begin
Active := False;
TableName := 'desa';
Filter := 'KODE_KECAMATAN='+QuotedStr(tkec);
Filtered:=True;
Active := True;
First;
end;
cbT3.Clear;
i:=0;
idx:=-1;
while (not ZTable1.Eof) do
begin
cbT3.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
if (kode=ZTable1.FieldByName('KODE_DESA').AsString) and (kode<>'') then
idx:=i;
ZTable1.Next;
Inc(i);
end;
cbT3.ItemIndex:=idx;
//TPS
with ZTable1 do
begin
Active := False;
TableName := 'daftar_tps';
Filter := 'KODE_DESA='+QuotedStr(kode);
Filtered:=True;
Active := True;
First;
end;
cbT4.Clear;
i:=0;
idx:=-1;
while (not ZTable1.Eof) do
begin
cbT4.Items.Add(ZTable1.FieldByName('NAMA').AsString);
if (idtps=ZTable1.FieldByName('id_tps').AsString) and (kode<>'') then
idx:=i;
ZTable1.Next;
Inc(i);
end;
cbT4.ItemIndex:=idx;
end;
GroupBox4.Visible:=true;
GroupBox1.Enabled:=false;
Button2.Enabled:=false;
//cbT1.Enabled:=false;

if (skodekab<>'00') then cbt1.Enabled:=false;
if (skodekec<>'') then cbt2.Enabled:=false;
end;

procedure TForm3.Timer1Timer(Sender: TObject);
begin
if label2.Visible=True then
   Label2.Visible:=False
else
   Label2.Visible:=True;
end;

procedure TForm3.Button5Click(Sender: TObject);
var
  acuan,thn: String;
  hari, bulan, tahun, hari_acuan, bulan_acuan, tahun_acuan, usia: Integer;
  temp: TStrings;
begin
  with ZQuery1 do begin
      Active:=false;
      sql.Clear;
      SQL.Text:='SELECT * FROM info_tanggal WHERE AKTIF = 1';
      Active:=true;
      acuan := fieldByName('TANGGAL').AsString;
    end;
  //showMessage(acuan);
  temp := TStringList.Create;
  try
      ExtractStrings(['/'], [], PChar(acuan), temp);
//      showmessage(temp[1]);
      if(temp.Count < 3) then begin
        temp := TStringList.Create;
        ExtractStrings(['-'], [], PChar(acuan), temp);
      end;
      hari_acuan := StrToInt(temp[1]);
      bulan_acuan := StrToInt(temp[0]);
      tahun_acuan := StrToInt(temp[2]);
    finally
      temp.Free;
    end;
  hari := 0;
  bulan:= 0;
  tahun:= 0;

  if ((cbTgl.ItemIndex>0) and (cbBulan.ItemIndex>0) and (txtTahun.Text<>'')) then begin
    hari:=strtoint(cbTgl.Items[cbTgl.ItemIndex]);
    bulan:=strtoint(cbBulan.Items[cbBulan.ItemIndex]);
    thn:=txtTahun.Text;
    if(Length(thn) = 2) then
    begin
      thn:= '19'+thn;
    end;
    tahun := strtoint(thn);
    usia := tahun_acuan - tahun;
    if(bulan_acuan < bulan) then
      usia := usia - 1
    else if(bulan_acuan = bulan) then begin
      if(hari_acuan < hari) then
        usia:= usia -1;
    end;
    txtUsia.Text:= IntToStr(usia);
  end else
    showMessage('silakan masukkan tanggal lahir dengan lengkap');
end;

procedure TForm3.sgHistoryClick(Sender: TObject);
begin
if (sgHistory.Row>0) and (sgHistory.Cells[1,1]<>'') then begin
    kembaliButton.Enabled:=True;
  end
    else
  begin
    kembaliButton.Enabled:=false;
  end;
end;

procedure TForm3.kembaliButtonClick(Sender: TObject);
var
idtgl,que,tanggal:string;
buttonselected:integer;
begin
  buttonSelected := MessageDlg('Apakah anda yakin akan mengembalikan data sesuai histori?',mtConfirmation , mbOKCancel, 0);
  if buttonSelected = mrOK  then begin
  tanggal := FormatDateTime('yyyy-mm-dd hh:mm:ss', Now());
  //editTps := TeditTps.Create(Application,btps[sgTPS.Row].ID);
  //showmessage(daftarHistori[(sgHistory.row-1)]);

  //masukin ke histori
  //---------------HISTORY
      with ZTable1 do begin
        active:=false;
        TableName:=ptabel;
        Filter:='ID_USER='+QuotedStr(pid);
        Filtered:=true;
        Active:=true;
        First;
      end;
      //showmessage(ptabel);
      with zquery1 do begin
          active:=false;
          sql.Clear;
          sql.Text:='INSERT INTO history (ID_USER,NIK,NKK,NAMA,TEMPAT,TANGGAL,IBU,AGAMA,KAWIN,KELAMIN,KELAINAN,ALAMAT,RW,RT,DUSUN,KET,ID_TANGGAL,TPS,STATUS1,STATUS2,GANDA,STATUS3,STATUSP,ALASANP,CACAT,PENDIDIKAN,PEKERJAAN,STATUSR,WN,USERNAME,KET_AKSI) VALUES ('+
            QuotedStr(tid+'-'+ZTable1.FieldByName('ID_USER').AsString)+','+
            QuotedStr(ZTable1.FieldByName('NIK').AsString)+','+
            QuotedStr(ZTable1.FieldByName('NKK').AsString)+','+
            QuotedStr(ZTable1.FieldByName('NAMA').AsString)+','+
            QuotedStr(ZTable1.FieldByName('TEMPAT').AsString)+','+
            QuotedStr(ZTable1.FieldByName('TANGGAL').AsString)+','+
            QuotedStr(ZTable1.FieldByName('IBU').AsString)+','+
            QuotedStr(ZTable1.FieldByName('AGAMA').AsString)+','+
            QuotedStr(ZTable1.FieldByName('KAWIN').AsString)+','+
            QuotedStr(ZTable1.FieldByName('KELAMIN').AsString)+','+
            QuotedStr(ZTable1.FieldByName('KELAINAN').AsString)+','+
            QuotedStr(ZTable1.FieldByName('ALAMAT').AsString)+','+
            QuotedStr(ZTable1.FieldByName('RW').AsString)+','+
            QuotedStr(ZTable1.FieldByName('RT').AsString)+','+
            QuotedStr(ZTable1.FieldByName('DUSUN').AsString)+','+
            QuotedStr(ZTable1.FieldByName('KET').AsString)+','+
            ZTable1.FieldByName('ID_TANGGAL').AsString+','+
            ZTable1.FieldByName('TPS').AsString+','+
            QuotedStr(ZTable1.FieldByName('STATUS1').AsString)+','+
            QuotedStr(ZTable1.FieldByName('STATUS2').AsString)+','+
            QuotedStr(ZTable1.FieldByName('GANDA').AsString)+','+
            QuotedStr(ZTable1.FieldByName('STATUS3').AsString)+','+
            QuotedStr(ZTable1.FieldByName('STATUSP').AsString)+','+
            QuotedStr(ZTable1.FieldByName('ALASANP').AsString)+','+
            QuotedStr(ZTable1.FieldByName('CACAT').AsString)+','+
            QuotedStr(ZTable1.FieldByName('PENDIDIKAN').AsString)+','+
            QuotedStr(ZTable1.FieldByName('PEKERJAAN').AsString)+','+
            QuotedStr(ZTable1.FieldByName('STATUSR').AsString)+','+
            QuotedStr(ZTable1.FieldByName('WN').AsString)+','+
            QuotedStr(username)+','+
            QuotedStr('0')+')';
          ExecSQL;
      end;

  //dari histori balik lagi
  //masuking id tanggal
  with ZQuery1 do  //masukkin nih gan
        begin
          Active:=False;
          sql.Clear;
          sql.Text := 'INSERT INTO tanggal (TANGGAL) VALUES ("'+tanggal+'")';
          ExecSQL;
        end;
        with ZQuery1 do  //masukkin nih gan
        begin
          Active:=False;
          sql.Clear;
          sql.Text := 'SELECT * FROM tanggal ORDER BY ID_TANGGAL DESC';
          Active:=True;
          idtgl := FieldByName('ID_TANGGAL').AsString;  //dapat ID_TGL
        end;
  //ambil dari histori
  with ZTable1 do begin
        active:=false;
        TableName:='history';
        Filter:='ID_HISTORY='+QuotedStr(daftarHistori[(sgHistory.row-1)]);
        Filtered:=true;
        Active:=true;
        First;

    //update
    que:='UPDATE '+ptabel+' SET '+
        '`NIK`='+QuotedStr(ZTable1.FieldByName('NIK').AsString)+
        ',`NKK`='+QuotedStr(ZTable1.FieldByName('NKK').AsString)+
        ',`NAMA`='+QuotedStr(ZTable1.FieldByName('NAMA').AsString)+
        ',`TEMPAT`='+QuotedStr(ZTable1.FieldByName('TEMPAT').AsString)+
        ',`TANGGAL`='+QuotedStr(ZTable1.FieldByName('TANGGAL').AsString)+
        ',`AGAMA`='+QuotedStr(ZTable1.FieldByName('AGAMA').AsString)+
        ',`KAWIN`='+QuotedStr(ZTable1.FieldByName('KAWIN').AsString)+
        ',`KELAMIN`='+QuotedStr(ZTable1.FieldByName('KELAMIN').AsString)+
        ',`KELAINAN`='+QuotedStr(ZTable1.FieldByName('KELAINAN').AsString)+
        ',`ALAMAT`='+QuotedStr(ZTable1.FieldByName('ALAMAT').AsString)+
        ',`RW`='+QuotedStr(ZTable1.FieldByName('RW').AsString)+
        ',`RT`='+QuotedStr(ZTable1.FieldByName('RT').AsString)+
        ',`DUSUN`='+QuotedStr(ZTable1.FieldByName('DUSUN').AsString)+
        ',`KET`='+QuotedStr(ZTable1.FieldByName('KET').AsString)+
        ',`ID_TANGGAL`='+idtgl+
        ',`STATUS1`='+QuotedStr(ZTable1.FieldByName('STATUS1').AsString)+
        ',`STATUS2`='+QuotedStr(ZTable1.FieldByName('STATUS2').AsString)+
        ',`STATUS3`='+QuotedStr(ZTable1.FieldByName('STATUS3').AsString)+
        ',`TPS`='+QuotedStr(ZTable1.FieldByName('TPS').AsString)+
        ',`STATUSP`='+QuotedStr(ZTable1.FieldByName('STATUSP').AsString)+
        ',`ALASANP`='+QuotedStr(ZTable1.FieldByName('ALASANP').AsString)+
        ',`CACAT`='+QuotedStr(ZTable1.FieldByName('CACAT').AsString)+
        ',`PENDIDIKAN`='+QuotedStr(ZTable1.FieldByName('PENDIDIKAN').AsString)+
        ',`PEKERJAAN`='+QuotedStr(ZTable1.FieldByName('PEKERJAAN').AsString)+
        ',`STATUSR`='+QuotedStr(ZTable1.FieldByName('STATUSR').AsString)+
        ',`WN`='+QuotedStr(ZTable1.FieldByName('WN').AsString);
    que:=que+' WHERE ID_USER = '+pid;
    end;
    //showmessage(que);
    with Zquery1 do begin
      SQL.Text:=que;
      ExecSQL;
    end;
      ShowMessage('Data pemilih telah berhasil dikembalikan.');
      Close;
    end;
end;

function TForm3.Hitung:Integer;
var
  acuan,thn: String;
  hari, bulan, tahun, hari_acuan, bulan_acuan, tahun_acuan, usia: Integer;
  temp: TStrings;
begin
  with ZQuery1 do begin
      Active:=false;
      sql.Clear;
      SQL.Text:='SELECT * FROM info_tanggal WHERE AKTIF = 1';
      Active:=true;
      acuan := formatDateTime('mm/dd/yyyy',fieldByName('TANGGAL').AsDateTime);
    end;
  //showMessage(acuan);
  temp := TStringList.Create;
  try
      ExtractStrings(['/'], [], PChar(acuan), temp);
//      showmessage(temp[1]);
      if(temp.Count < 3) then begin
        temp := TStringList.Create;
        ExtractStrings(['-'], [], PChar(acuan), temp);
      end;
      hari_acuan := StrToInt(temp[1]);
      bulan_acuan := StrToInt(temp[0]);
      tahun_acuan := StrToInt(temp[2]);
    finally
      temp.Free;
    end;
  hari := 0;
  bulan:= 0;
  tahun:= 0;

  if ((cbTgl.ItemIndex>0) and (cbBulan.ItemIndex>0) and (txtTahun.Text<>'')) then begin
    hari:=strtoint(cbTgl.Items[cbTgl.ItemIndex]);
    bulan:=strtoint(cbBulan.Items[cbBulan.ItemIndex]);
    thn:=txtTahun.Text;
    if(Length(thn) = 2) then
    begin
      thn:= '19'+thn;
    end;
    tahun := strtoint(thn);
    usia := tahun_acuan - tahun;
    if(bulan_acuan < bulan) then
      usia := usia - 1
    else if(bulan_acuan = bulan) then begin
      if(hari_acuan < hari) then
        usia:= usia -1;
    end;
    result:= usia;
    //txtUsia.Text:= IntToStr(usia);
  end else result:= 0;
end;

procedure TForm3.cbTPSChange(Sender: TObject);
begin
  stps:='0';
  if(cbTPS.Items[cbTPS.ItemIndex] = '') then begin
    stps := '0';
  end
  else begin
    with ZTable1 do begin
      active:=false;
      TableName:='daftar_tps';
      Filter:='KODE_DESA='+QuotedStr(sdesa)+' AND NAMA='+QuotedStr(cbTPS.Items[cbTPS.ItemIndex]);
      //showmessage('KODE_DESA='+QuotedStr(sdesa)+' AND NAMA='+QuotedStr(cbTPS.Items[cbTPS.ItemIndex]));
      Filtered:=true;
      Active:=true;
      First;
      while not Eof do begin
        stps:=fieldByName('ID_TPS').asString;
        next;
      end;
    end;
  end;
end;

end.
